import { createApp } from 'vue';
import { createRouter, createWebHashHistory } from 'vue-router';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import App from './App.vue';
import routes from './routerConfig.js';

// Mock electronAPI for browser development
if (typeof window.electronAPI === 'undefined') {
  console.warn('Running in browser development mode - using mock electronAPI');

  window.electronAPI = {
    getWord: async word => {
      console.log('Mock getWord called with:', word);
      // Mock response for development
      return {
        success: true,
        data: {
          id: 1,
          word: word,
          phonetic: '/ˈæpl/',
          pronounce: 'https://dict.youdao.com/dictvoice?audio=apple&type=1',
          trans: 'n. 苹果；苹果公司',
          ielst: 'YES',
          ok: 'NO',
          uk_speech: 'https://dict.youdao.com/dictvoice?audio=apple&type=1',
          us_speech: 'https://dict.youdao.com/dictvoice?audio=apple&type=2',
          definitions: [
            {
              part_of_speech: 'n.',
              definition: '苹果树的果实',
            },
          ],
          examples: [
            {
              example: 'I ate an apple for breakfast.',
              translation: '我早餐吃了一个苹果。',
            },
          ],
        },
      };
    },
    searchWords: async pattern => {
      console.log('Mock searchWords called with:', pattern);
      return {
        success: true,
        data: [
          { word: 'apple', phonetic: '/ˈæpl/', trans: 'n. 苹果' },
          { word: 'application', phonetic: '/ˌæplɪˈkeɪʃn/', trans: 'n. 应用程序' },
          { word: 'apply', phonetic: '/əˈplaɪ/', trans: 'v. 申请' },
        ],
      };
    },
    searchWordsByPrefix: async ({ prefix, limit }) => {
      console.log('Mock searchWordsByPrefix called with:', prefix, limit);
      return {
        success: true,
        data: [
          { word: prefix + 'example1', phonetic: '/ˈɛksæmpəl/', trans: 'n. 示例' },
          { word: prefix + 'example2', phonetic: '/ˈɛksæmpəl/', trans: 'n. 示例' },
        ],
      };
    },
    searchWordsBySuffix: async ({ suffix, limit }) => {
      console.log('Mock searchWordsBySuffix called with:', suffix, limit);
      return {
        success: true,
        data: [
          { word: 'test' + suffix, phonetic: '/tɛst/', trans: 'n. 测试' },
          { word: 'rest' + suffix, phonetic: '/rɛst/', trans: 'n. 休息' },
        ],
      };
    },
    searchSimilarWords: async ({ word, limit }) => {
      console.log('Mock searchSimilarWords called with:', word, limit);
      return {
        success: true,
        data: [
          { word: word + 'like', phonetic: '/ˈlaɪk/', trans: 'adj. 相似的' },
          { word: 'similar', phonetic: '/ˈsɪmɪlər/', trans: 'adj. 类似的' },
        ],
      };
    },
    getWords: async ({ limit, offset }) => {
      console.log('Mock getWords called with:', limit, offset);
      return {
        success: true,
        data: [
          { word: 'apple', phonetic: '/ˈæpl/', trans: 'n. 苹果' },
          { word: 'banana', phonetic: '/bəˈnænə/', trans: 'n. 香蕉' },
          { word: 'orange', phonetic: '/ˈɔrɪndʒ/', trans: 'n. 橙子' },
        ],
      };
    },
    addQueryHistory: async word => {
      console.log('Mock addQueryHistory called with:', word);
      return { success: true, data: 1 };
    },
    getQueryHistory: async limit => {
      console.log('Mock getQueryHistory called with:', limit);
      return {
        success: true,
        data: [
          { id: 1, word: 'apple', queried_at: new Date().toISOString() },
          { id: 2, word: 'banana', queried_at: new Date().toISOString() },
        ]
      };
    },
    clearQueryHistory: async () => {
      console.log('Mock clearQueryHistory called');
      return { success: true, data: true };
    },
    updateKnownStatus: async ({ word, status }) => {
      console.log('Mock updateKnownStatus called with:', word, status);
      return { success: true, data: true };
    },
    updateIeltsTag: async ({ word, tag }) => {
      console.log('Mock updateIeltsTag called with:', word, tag);
      return { success: true, data: true };
    },
  };
}

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 创建应用实例
const app = createApp(App);

// 使用路由和ElementPlus
app.use(router);
app.use(ElementPlus);

// 挂载应用
app.mount('#app');
